﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;


namespace BidFunLib.DAO
{
    class BidLoggerDAO
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidLoggerDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public BidLogger getBidLoggerByLabel(String label)
        {
            BidLogger bidLogger = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidLogger> objects = (IList<BidLogger>)session.CreateQuery("from BidLogger bo where bo.Label= ?")
                .SetString(0, label)
                .List<BidLogger>();

                if (objects != null && objects.Count > 0)
                {
                    bidLogger = objects.First();
                }
                transaction.Commit();
                session.Close();
                return bidLogger;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BidLogger getBidLoggerById(Int32 id)
        {
            BidLogger bidLogger = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidLogger> loggers = (IList<BidLogger>)session.CreateQuery("from BidLogger bo where bo.IdBidLogger=?")
                .SetInt32(0, id)
                .List<BidLogger>();

                if (loggers != null && loggers.Count > 0)
                {
                    bidLogger = loggers.First();
                }
                transaction.Commit();
                session.Close();
                return bidLogger;
            }
        }

        /// <summary>
        /// Retrouve toutes les encheres d'en vente
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <returns>IList<BidLogger></returns>
        public IList<BidLogger> getAllBidLoggerForSpecificIdBidSale(Int32 idBidSale)
        {
            IList<BidLogger> loggers = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                
                loggers = (IList<BidLogger>)session.CreateQuery("from BidLogger bo where bo.IdBidSale=? order by bo.IdBidLogger asc")
                .SetInt32(0, idBidSale)
                .List<BidLogger>();

                foreach (BidLogger log in loggers)
                {
                    log.BidUser = (BidUser)session.CreateQuery("from BidUser bo where bo.IdBidUser=? ").SetInt32(0, log.IdBidUser).List<BidUser>().First<BidUser>();
                    log.BidSale = (BidSale)session.CreateQuery("from BidSale bo where bo.IdBidSale=? ").SetInt32(0, log.IdBidSale).List<BidSale>().First<BidSale>();
                    log.BidOperatorType = (BidOperatorType)session.CreateQuery("from BidOperatorType bo where bo.IdBidOperatorType=? ").SetInt32(0, log.IdBidOperatorType).List<BidOperatorType>().First<BidOperatorType>();

                }

                transaction.Commit();
                session.Close();


                return loggers;
            }
        }

        /// <summary>
        /// Retrouve toutes les encheres d'en vente
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <returns>IList<BidLogger></returns>
        /*public IList<BidLogger> getAllBidLoggerForSpecificIdBidSale(Int32 idBidSale, String orderString)
        {
            IList<BidLogger> loggers = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                loggers = (IList<BidLogger>)session.CreateQuery("from BidLogger bo where bo.IdBidSale= ? " + orderString)
                .SetInt32(0, idBidSale)
                .List<BidLogger>();

                transaction.Commit();
                session.Close();
                return loggers;
            }
        }*/

        /// <summary>
        /// Retourne le nombre d'enchere d'une vente
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <returns></returns>
        public Int32 getNbBidLoggerForSpecificIdBidSale(Int32 idBidSale)
        {
            Int32 nbEnchere = 0;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                nbEnchere = (Int32)session.CreateQuery("count (*) from BidLogger bo where bo.IdBidSale= ?")
                .SetInt32(0, idBidSale)
                .List().Count;

                transaction.Commit();
                session.Close();
                return nbEnchere;
            }
        }



        /// <summary>
        /// Save the object 
        /// </summary>
        public BidLogger saveOrUpdateBidLogger(BidLogger bidLogger)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                if (bidLogger.IdBidLogger ==-1)
                {
                  //  log.Debug("Saving BidLogger " + bidLogger.Amount + "["+bidLogger.BidUser.Pseudo+"]");
                    Int32 newID = (Int32)session.Save(bidLogger);
                    bidLogger.IdBidLogger = newID;
                  //  log.Debug("BidLogger " + bidLogger.Amount + "["+bidLogger.BidUser.Pseudo+"] saved !");
                }
                else
                {
                  //  log.Debug("Updating BidLogger " + bidLogger.Amount + "["+bidLogger.BidUser.Pseudo+"]");
                    session.Update(bidLogger);

                  //  log.Debug("BidLogger " + bidLogger.Amount + "[" + bidLogger.BidUser.Pseudo + "] updated !");
                }
                transaction.Commit();
                session.Close();
            }

            return bidLogger;
        }

    }
}
